
#import "UserTorrentDetailsViewController.h"
#import "TorrentDetailsViewController.h"

@implementation UserTorrentDetailsViewController

//
// torrent info
//
@synthesize torrentInfo														= _torrentInfo;

//
// type
//
@synthesize type															= _type;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super initWithStyle:UITableViewStyleGrouped];
	if(!self)
		return nil;
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.torrentInfo														= nil;
	self.type																= nil;
	
	[super dealloc];
}

//
// did load
//
- (void)viewDidLoad
{
	[super viewDidLoad];
	self.title																= NSLocalizedStringFromTable(([NSString stringWithFormat:@"%@ Details", self.type]), @"UserTorrentDetailsViewController", nil);
}

//
// get section count
//
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
	return 1;
}

//
// get row count
//
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	if([self.type isEqualToString:@"Uploaded"])
		return 7;
	
	if([self.type isEqualToString:@"Seeding"] || [self.type isEqualToString:@"Leeching"])
		return 8;
	
	if([self.type isEqualToString:@"Completed"])
		return 6;
	
	return 6;
}

//
// get title
//
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
	return self.torrentInfo.title;
}

//
// get cell
//
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	UITableViewCell* theCell												= nil;
	if(indexPath.row == 0)
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"d>"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"d>"] autorelease];
		
		theCell.textLabel.text												= self.torrentInfo.title;
		theCell.accessoryType												= UITableViewCellAccessoryDisclosureIndicator;
	}
	else if(indexPath.row == 1)
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"v1"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"v1"] autorelease];
		
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Category", @"UserTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= self.torrentInfo.category;
		theCell.accessoryType												= UITableViewCellAccessoryNone;
		theCell.selectionStyle												= UITableViewCellSelectionStyleNone;
	}
	else if(indexPath.row == 2)
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"v1"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"v1"] autorelease];
		
		if([self.type isEqualToString:@"Uploaded"] || [self.type isEqualToString:@"Seeding"] || [self.type isEqualToString:@"Leeching"])
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Size", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.size;
		}
		else
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Uploaded", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.uploadedSize;
		}
		
		theCell.accessoryType												= UITableViewCellAccessoryNone;
		theCell.selectionStyle												= UITableViewCellSelectionStyleNone;
	}
	else if(indexPath.row == 3)
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"v1"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"v1"] autorelease];
		
		if([self.type isEqualToString:@"Uploaded"] || [self.type isEqualToString:@"Seeding"] || [self.type isEqualToString:@"Leeching"])
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Seeder", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= [NSString stringWithFormat:@"%d", self.torrentInfo.seederCount];
		}
		else if([self.type isEqualToString:@"Completed"])
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Seed Time", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.seedTime;
		}
		else
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Downloaded", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.downloadedSize;
		}
		
		theCell.accessoryType												= UITableViewCellAccessoryNone;
		theCell.selectionStyle												= UITableViewCellSelectionStyleNone;
	}
	else if(indexPath.row == 4)
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"v1"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"v1"] autorelease];
		
		if([self.type isEqualToString:@"Uploaded"] || [self.type isEqualToString:@"Seeding"] || [self.type isEqualToString:@"Leeching"])
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Leecher", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= [NSString stringWithFormat:@"%d", self.torrentInfo.leecherCount];
		}
		else if([self.type isEqualToString:@"Completed"])
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Leech Time", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.leechTime;
		}
		else
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Share Ratio", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.shareRatio;
		}
		
		theCell.accessoryType												= UITableViewCellAccessoryNone;
		theCell.selectionStyle												= UITableViewCellSelectionStyleNone;
	}
	else if(indexPath.row == 5)
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"v1"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"v1"] autorelease];
		
		if([self.type isEqualToString:@"Uploaded"] || [self.type isEqualToString:@"Seeding"] || [self.type isEqualToString:@"Leeching"])
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Uploaded", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.uploadedSize;
		}
		else if([self.type isEqualToString:@"Completed"])
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Complete Time", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.completeTime;
		}
		else
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Leech Time", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.leechTime;
		}
		
		theCell.accessoryType												= UITableViewCellAccessoryNone;
		theCell.selectionStyle												= UITableViewCellSelectionStyleNone;
	}
	else if(indexPath.row == 6)
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"v1"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"v1"] autorelease];
		
		if([self.type isEqualToString:@"Uploaded"])
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Seed Time", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.seedTime;
		}
		else
		{
			theCell.textLabel.text											= NSLocalizedStringFromTable(@"Downloaded", @"UserTorrentDetailsViewController", nil);
			theCell.detailTextLabel.text									= self.torrentInfo.downloadedSize;
		}
		
		theCell.accessoryType												= UITableViewCellAccessoryNone;
		theCell.selectionStyle												= UITableViewCellSelectionStyleNone;
	}
	else if(indexPath.row == 7)
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"v1"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"v1"] autorelease];
		
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Share Ratio", @"UserTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= self.torrentInfo.shareRatio;
		theCell.accessoryType												= UITableViewCellAccessoryNone;
		theCell.selectionStyle												= UITableViewCellSelectionStyleNone;
	}
	
	return theCell;
}

//
// select row
//
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
	if(indexPath.row == 0)
	{
		TorrentDetailsViewController* torrentDetailsViewController			= [[[TorrentDetailsViewController alloc] init] autorelease];
		torrentDetailsViewController.torrentInfo							= self.torrentInfo;
		[self.navigationController pushViewController:torrentDetailsViewController animated:YES];
	}
}

@end